# coding=utf-8
import requests
from pyquery import PyQuery as pq

#安装插件
# pip3.7 install requests
# pip3.7 install PyQuery

# 登录地址
login_url = "https://www.yiichina.com/login"
# 签到地址
url2 = "https://www.yiichina.com/registration"

# 构造登陆的header头
headers = {
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
    'Cache-Control': "no-cache",
    'Connection': "keep-alive",
    'Content-Length': "250",
    'Content-Type': "application/x-www-form-urlencoded",
    'Host': "www.yiichina.com",
    'Origin': "https://www.yiichina.com",
    'Pragma': "no-cache",
    'Referer': "https://www.yiichina.com/login",
    'Upgrade-Insecure-Requests': "1",
    'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/67.0.3396.99 Safari/537.36 "
}

# 构造签到的header头
headers2 = {
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'Accept-Encoding': "gzip, deflate, br",
    'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
    'Cache-Control': "no-cache",
    'Connection': "keep-alive",
    'Content-Length': "250",
    'Content-Type': "application/x-www-form-urlencoded",
    'Host': "www.yiichina.com",
    'Origin': "https://www.yiichina.com",
    'Pragma': "no-cache",
    'Referer': "https://www.yiichina.com/",
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
    'X-CSRF-Token': "jiZGgvngdp0HiiclI4SheJlJr5-lVyymIFgbfE1ZUw33FWv2mI5DzyrJFUFHt8tO_h3az84GH9xEHEtFCh8cfg==",
    'X-Requested-With': "XMLHttpRequest"
}


# 登陆方法和签到
def login_sign():
    r_session = requests.Session()
    # 请求页面
    page = r_session.get(login_url)
    # 请求获得的内容
    content = pq(page.content)

    # 获取csrf_token
    item1 = content('meta[name=csrf-token]').items()
    csrf = ''
    for nurl in item1:
        csrf = nurl.attr('content')

    data = {
        "_csrf": csrf,
        "LoginForm[username]": "2524747643@qq.com",#帐号
        "LoginForm[password]": "qq2524747643",#密码
        'LoginForm[rememberMe]': "0",
        "LoginForm[rememberMe]": "1",
        "login - button": ""
    }
    # 完成用户登录
    response1 = r_session.post(login_url, data=data, headers=headers)
    # 请求获得的内容
    content = pq(response1.content)

    # 获取csrf_token
    item1 = content('meta[name=csrf-token]').items()
    csrf = ''
    for nurl in item1:
        csrf = nurl.attr('content')

    data2 = {
        "_csrf": csrf
    }
    # 模拟签到请求
    response2 = r_session.post(url2, data=data2, headers=headers2)
    content = pq(response2.content)
    items = content('p').items()
    for a in items:
        print(a.html())


if __name__ == "__main__":
    login_sign()
